/* eslint-disable no-nested-ternary */
/* eslint-disable default-case */
namespace ui {
	/*
	class uiChar extends game.GameChar
	{

		public constructor() {
			super();
			//this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this)
			//this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.onRemoveFromStage,this)
		}

		// onAddToStage(): void {
		// 	this.restore();
		// }
		// restore(){
		// 	this.baseChar.restore();
		// }
		//$onRemoveFromStage(): void {
		//	super.$onRemoveFromStage()
		//	this.baseChar.release();
		//}

	}
	*/

	export class Char extends eui.Component {
		// char = new uiChar()
		char: game.GameChar;

		touchX: number;

		touchY: number;

		public constructor() {
			super();
			this.char = Pool.ins().pop('ui.Char', 'uiChar', game.GameChar);
			// this.char.baseChar.isShow = true;
			this.addChild(this.char);
		}

		walk(x: number, y: number): void {
			// this.setWalkDir(x, y);
			this.char.setAction('walk', -1, true, 0, 1750);
			egret.Tween.get(this.char)
				.to({ x, y }, 1750)
				.call(() => {
					EventDispatcherBetweenSkins.instance().dispatchEventWith(
						DlgClimbTower.char_stand,
					);
					this.char.stand();
				});
		}

		setWalkDir(x: number, y: number): void {
			// +:1; 0:0; -:-1;
			const dx = x - this.char.x > 0 ? 1 : x - this.char.x < 0 ? -1 : 0;
			const dy = y - this.char.y > 0 ? 1 : y - this.char.y < 0 ? -1 : 0;
			switch (dx) {
				case 0: {
					switch (dy) {
						case 1: {
							this.char.setDir(4, true);
							break;
						}
						case -1: {
							this.char.setDir(6, true);
							break;
						}
					}
					break;
				}
				case 1: {
					switch (dy) {
						case 0: {
							this.char.setDir(7, true);
							break;
						}
						case 1: {
							this.char.setDir(0, true);
							break;
						}
						case -1: {
							this.char.setDir(3, true);
							break;
						}
					}
					break;
				}
				case -1: {
					switch (dy) {
						case 0: {
							this.char.setDir(5, true);
							break;
						}
						case 1: {
							this.char.setDir(1, true);
							break;
						}
						case -1: {
							this.char.setDir(2, true);
							break;
						}
					}
					break;
				}
			}
		}

		override childrenCreated(): void {
			super.childrenCreated();

			this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
			this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
		}

		deleteTouchEvent(): void {
			this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
			this.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
		}

		private onTouchBegin(e: egret.TouchEvent): void {
			this.touchX = e.stageX;
			this.touchY = e.stageY;
		}

		private onTouchEnd(e: egret.TouchEvent): void {
			const dir = this.char.getDir();
			const offsetX = e.stageX - this.touchX;
			if (offsetX === 0) {
				this.char.updateDir(0);
				this.char.baseChar.playActionOnce('attack', 1, 0, () => {
					this.char.stand();
				});
			} else if (offsetX > 0) {
				this.char.updateDir(dir - 1);
			} else {
				this.char.updateDir(dir + 1);
			}
			this.touchX = 0;
			this.touchY = 0;
		}

		onChangedBody(h: number): void {
			const { width } = this.char.baseChar;
			const { height } = this.char.baseChar;

			const scale = Math.min(this.width / width, this.height / height);
			this.char.scaleX = scale;
			this.char.scaleY = scale;

			this.char.x = this.width / 2;
			this.char.y = this.height;
		}

		createDisplay(display: SceneDisplay, dir = 0): void {
			// this.char.onChangedBodyFunc = this.onChangedBody.bind(this);
			let dis = display;
			if (dir > 0) {
				dis = Utils.deepCopy(display);
				dis.dir = dir;
			}
			this.char.visible = false;
			this.char.createDisplay(dis);
			// this.char.updateDir(dir);
			this.char.baseChar.playActionOnce('attack', 1, 0, () => {
				this.char.stand();
			});
			this.char.visible = true;
			// this.char.stand();
			this.char.lbName.visible = false;
		}

		hideDisplay(): void {
			this.char.visible = false;
		}

		onRemoveFromStage(): void {
			super.$onRemoveFromStage();
			Pool.ins().push(this.char);
			this.char.release();
			this.char = null;
		}
	}
}
